<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;

/**
 * Class GroupPost
 * @package App\Models
 * @mixin \Eloquent
 */
class GroupPost extends Model
{
    protected $table = 'group_post';


    public function setImagesAttribute($images)
    {
        if (is_array($images)) {
            $this->attributes['images'] = json_encode($images);
        }
    }

    public function getImagesAttribute($images)
    {
        return json_decode($images, true);
    }


    public static function disposeImages($images)
    {
        $img = [];
        $count = count($images);
        $type = 0;
        foreach ($images as $k => $v) {
            if ($k < 9) {
                if ($count == 1) {
                    $img[] = config('admin.upload.host') . $v . '_710x470';
                    $type = 1;
                } else if (in_array($count, [2, 4])) {
                    $img[] = config('admin.upload.host') . $v . '_310x310';
                    $type = 2;
                } else {
                    $img[] = config('admin.upload.host') . $v . '_200x200';
                    $type = 3;
                }
            }

        }
        return ['type'=>$type,'list'=>$img];
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function group()
    {
        return $this->belongsTo(Group::class);
    }
}
